#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <tinyxml.h>
#include "../include/Engine.hpp"
#include "../include/EcranTitre.hpp"
#include "../include/GUI/WindowBase.hpp"
#include "../include/GUI/Button.hpp"
#include "../include/GUI/Layer.hpp"
#include "../include/GUI/Onglets.hpp"
#include "../include/GUI/Checkbox.hpp"
#include "../include/GUI/ProgressBar.hpp"
#include "../include/GUI/Slide.hpp"


using namespace std;

double myRand(double min, double max)
{  return (double) (min + ((float) rand() / RAND_MAX * (max - min + 1.0)));
}

EcranTitre::EcranTitre()
{

}

int EcranTitre::Run (Engine &Moteur)
{
    sf::Event Event;
    bool Running = true;

    sf::Sprite cursor;
    cursor.SetImage(*Moteur.Graphic()->GetSystemImage(2));
    cursor.SetSubRect(sf::IntRect(0,0,30,30));

    sf::Music bgm;
    if(!bgm.OpenFromFile("Musics/BGM/Main_Title.ogg"))
    {
         //Erreur...
    }
    //bgm.Play();
    bgm.SetVolume(50);
    bgm.SetLoop(true);

    sf::Sprite background;
    background.SetImage(*Moteur.Graphic()->GetSystemImage(1));
    background.SetPosition(0,0);

    GUI::WindowBase window1(Moteur, "Window 1");
    window1.SetPosition((int)((Moteur.App().GetWidth()-window1.GetDimensions().width)/2),250);
    GUI::WindowBase window2(Moteur, "Window 2");
    window2.SetDimensions(300,300);
    window2.SetPosition((int)((Moteur.App().GetWidth()-window2.GetDimensions().width)/4),(int)(250/2));
    GUI::WindowBase window3(Moteur, "Window 3");
    window3.SetPosition((int)((Moteur.App().GetWidth()-window3.GetDimensions().width)/8),(int)(250/4));

//    window1.Open();
//    window2.Close();
//    window3.Open();

    GUI::Layer gui(Moteur);
    gui.SetWidget(&window1);
    gui.SetWidget(&window2);
    gui.SetWidget(&window3);

    GUI::Button bouton1(Moteur, "Teste1");
    bouton1.SetConteneurPosition(0,0);
    GUI::Button bouton2(Moteur, "Teste2");
    bouton2.SetConteneurPosition(0,0);
    GUI::Button bouton3(Moteur, "OK");
    bouton3.SetConteneurPosition(0,48);

    GUI::Button bouton4(Moteur, "OK");
    bouton4.SetConteneurPosition(0,30);
    GUI::Button bouton5(Moteur, "OK");
    bouton5.SetConteneurPosition(0,60);
    GUI::Button bouton6(Moteur, "OK");
    bouton6.SetConteneurPosition(0,90);
    GUI::Button bouton7(Moteur, "OK");
    bouton7.SetConteneurPosition(0,120);

    GUI::Checkbox checkbox(Moteur, "Option 1");
    checkbox.SetConteneurPosition(0,0);
    GUI::Checkbox checkbox2(Moteur, "Option 2");
    checkbox2.SetConteneurPosition(0,24);
    GUI::Checkbox checkbox3(Moteur, "Option 1");
    checkbox3.SetConteneurPosition(0,0);
    GUI::Checkbox checkbox4(Moteur, "Option 2");
    checkbox4.SetConteneurPosition(0,24);
    GUI::Checkbox checkbox5(Moteur, "Option 2");
    checkbox5.SetConteneurPosition(0,48);
    GUI::ProgressBar progressbar(Moteur, 100, 100);
    progressbar.SetConteneurPosition(0,0);
    GUI::Slide slide(Moteur, 50, 100);
    slide.SetConteneurPosition(0,40);

    window1.SetWidget(&bouton1);
    window1.SetWidget(&bouton4);
    window1.SetWidget(&bouton5);
    window1.SetWidget(&bouton6);
    window1.SetWidget(&bouton7);
    window3.SetWidget(&checkbox3);
    window3.SetWidget(&checkbox4);
    window3.SetWidget(&checkbox5);

    GUI::Onglets onglets(Moteur);
    onglets.SetConteneurPosition(0,0);
    onglets.SetDimensions(81*3-18,200);
    onglets.AddOnglet("Onglet1");
    onglets.AddOnglet("Onglet2");
    onglets.AddOnglet("Onglet3");

    window2.SetWidget(&onglets);

    window1.Adjust();
    window3.Adjust();

    onglets.AddWidget(0, &bouton2);
    onglets.AddWidget(1, &progressbar);
    onglets.AddWidget(1, &slide);
    onglets.AddWidget(2, &checkbox);
    onglets.AddWidget(2, &checkbox2);
    onglets.AddWidget(2, &bouton3);

    bouton3.Center();
    checkbox.Center();
    checkbox2.Center();
    checkbox3.Center();
    checkbox4.Center();
    progressbar.Center();
    slide.Center();

    onglets.Adjust();
    window2.Adjust();
    onglets.Center(); // On centre notre objet

    while (Running)
    {
        //Verifying events
        while (Moteur.GetEvent(Event))
        {
            // Window closed
            if (Event.Type == sf::Event::Closed)
            {
                return (SCREEN_EXIT);
            }
            // Mouse moved
            if (Event.Type == sf::Event::MouseMoved)
            {
                cursor.SetPosition(Moteur.App().GetInput().GetMouseX(),Moteur.App().GetInput().GetMouseY());
            }
            // Key pressed
            if (Event.Type == sf::Event::KeyPressed)
            {
                switch (Event.Key.Code)
                {
                    case sf::Key::Return:
                        return (SCREEN_EXIT);
                    break;
                    case sf::Key::Numpad1:
                        if(!window1.IsOpen())
                            window1.Open();
                        else
                            window1.Close();
                    break;
                    case sf::Key::Numpad2:
                        if(!window2.IsOpen())
                            window2.Open();
                        else
                            window2.Close();
                    break;
                    case sf::Key::Numpad3:
                        if(!window3.IsOpen())
                            window3.Open();
                        else
                            window3.Close();
                    break;
                }
            }
            gui.UpdateEvent();
            progressbar.SetValue(slide.GetValue());

            if(checkbox.IsClicked())
            {
                checkbox2.SetCheck(false);
            }
            else if(checkbox2.IsClicked())
            {
                checkbox.SetCheck(false);
            }

            if(checkbox.IsCheck() and !checkbox2.IsCheck())
                bouton1.SetText("Option 1");
            else if(!checkbox.IsCheck() and checkbox2.IsCheck())
                bouton1.SetText("Option 2");
            else if(checkbox.IsCheck() and checkbox2.IsCheck())
                bouton1.SetText("Option 1&2");
            else
                bouton1.SetText("Teste");

            if(progressbar.IsMax())
                bouton2.SetText("MAX!!");
            else if(progressbar.GetText().GetString() != "")
                bouton2.SetText(progressbar.GetText().GetString());
            else
                bouton2.SetText("Teste2");
        }
        Moteur.Graphic()->Clear(sf::Color(255,255,255));

        Moteur.Graphic()->Draw(background);
        gui.Afficher();

        Moteur.Graphic()->Draw(cursor);

        Moteur.Graphic()->Display();
    }

    //Never reaching this point normally, but just in case, exit the application
    return (SCREEN_EXIT);
}
